package com.stu.student_back_end.controller;

import com.stu.student_back_end.common.Constants;
import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblDorm;
import com.stu.student_back_end.service.ITblDormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class DormController {

    @Autowired
    private ITblDormService dormService;

    /**
     * 分页查询宿舍
     * @param pageDTO
     * @return
     */
    @PostMapping("/d/query")
    public Result queryDorm(@RequestBody PageDTO pageDTO) {
        return Result.success(dormService.queryDorm(pageDTO));
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @PostMapping("/d/byId/{id}")
    public Result queryByIdDorm(@PathVariable("id") int id) {
        TblDorm dorm = dormService.getById(id);

        //判断是否查询成功
        if(dorm == null) return Result.success("查无数据！");

        //查询成功
        return Result.success(dorm);
    }

    /**
     * 更新宿舍数据
     * @param dorm
     * @return
     */
    @PostMapping("/d/update")
    public Result updateDorm(@RequestBody TblDorm dorm) {
        boolean updateById = dormService.updateById(dorm);

        if(!updateById) return Result.error(Constants.CODE_500,"修改失败！");

        return Result.success("修改成功！");
    }

    @GetMapping("/d/count")
    public Result queryCount() {
        return Result.success(dormService.queryDormCount());
    }
}
